<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Conjunto de funciones basicas para el funcionamiento de la extension de kohana
 *
 * @package    Basics
 * @author     Erik Beltran
 * @copyright  (c) 2009 Erik Beltran
 * @license    GNU PUBLIC
 *
 */
class fpp_Core {
  public static function CheckboxGroup($name,$checks,$check_checked=null,$styles=null){
    $ret = null;
    foreach($checks as $value => $label){
      $ret .= "<p>".form::checkbox("{$name}[$value]",TRUE,@$check_checked[$value])."{$label}</p>";
    }
    return $ret;  
  }
  public static function RadioGroup($name,$checks,$check_checked=null,$styles=null){
    $ret = null;
    foreach($checks as $value => $label){
      $checkradio = FALSE;
      if(isset($check_checked[$value])){
        $checkradio = TRUE;
      }
      $ret .= "<p>".form::radio("{$name}",$value,$checkradio)."{$label}</p>";
    }
    return $ret;  
  }
  
  public static function autoup(&$val){
    $val = strtoupper($val);
  }
  public static function tohuman(&$date){
    $datetime = date_create($date);
    $date = date_format($datetime, "d/m/Y H:i");
  }
  
  public static function tohuman2(&$date){
    $datetime = date_create($date." 00:00:00");
    $date = date_format($datetime, "d/m/Y");
  }
  
  public static function jlib(){
    if(@$_GET['AJAX_NO_LOAD'] == 'TRUE'){
      return null;
    }
    $str = null;
    $jsdir=Kohana::config('fpp_config.directory_js');
    $str .= "<script>baseurl= '".url::base(FALSE)."'</script>";
    $str .= html::script(array(
      $jsdir.'jquery-1.3.2.min.js',
      $jsdir.'ui.core.js',
      $jsdir.'jui/js/jquery-ui-1.7.2.custom.min.js',
      $jsdir.'jquerylib.js',
      $jsdir.'timepicker.js',
      $jsdir.'i18n/ui.datepicker-es.js',
      $jsdir.'ui.spinner.js',
    ));
    $str .=  html::stylesheet(array(
      $jsdir.'jui/css/le-frog/jquery-ui-1.7.2.custom.css',
      $jsdir.'ui.spinner.css',
    ));
    $str.=<<<EOF
<style type="text/css">
  /*demo page css*/
  body.nostyles{ font: 62.5% "Trebuchet MS", sans-serif; margin: 50px;}
  .demoHeaders { margin-top: 2em; }
  #dialog_link {padding: .4em 1em .4em 20px;text-decoration: none;position: relative;}
  #dialog_link span.ui-icon {margin: 0 5px 0 0;position: absolute;left: .2em;top: 50%;margin-top: -8px;}
  ul#icons {margin: 0; padding: 0; width:30px;}
  ul#icons li {margin: 2px; position: relative; padding: 4px 0; cursor: pointer; float: left;  list-style: none;}
  ul#icons span.ui-icon {float: left; margin: 0 4px;}
</style>
<script>
$(document).ready(function(){
  $('#dialog_link, ul#icons li').hover(
    function() { $(this).addClass('ui-state-hover'); }, 
    function() { $(this).removeClass('ui-state-hover'); }
  );
})
</script>
EOF;
    return $str;
  }

  public static function datedb(&$date,$time = TRUE){
    if($time ){
      list($d,$m,$y,$h,$M) = preg_split('/[: \/]/',$date);
      $date = date('Y-m-d H:i:s', mktime($h, $M, 0, $m, $d, $y));
    }else{
      list($d,$m,$y) = explode('/',$date);
      $date = date('Y-m-d', mktime(0, 0, 0, $m, $d, $y));
    }
  }

  public static function datedbnt(&$date){
    self::datedb($date,FALSE);
  }
  
  public static function fppmsg($msg,$error=FALSE){
  $state = $error == FALSE ? 'highlight' : 'error';
  $icon = $error == FALSE ? 'info' : 'alert';
return '
  <div class="ui-widget">
    <div class="ui-state-'.$state.' ui-corner-all" style="margin-top: 20px; padding: 0 .7em;"> 
    <p style="margin: 11px 0px 11px 0px;"><span class="ui-icon ui-icon-'.$icon.'" style="float: left; margin-right: .3em;"></span>
      '.$msg.'.
      </p>
    </div>
  </div>';
  }
  
  public static function datedb2unix($datetime){
    $expstr = explode(' ',$datetime);
    $dateexp = explode('-',$expstr[0]);
    $hourexp = explode(':',$expstr[1]);
    $timestamp = mktime($hourexp[0],$hourexp[1],$hourexp[2],$dateexp[1],$dateexp[2],$dateexp[0]);
    return $timestamp;
  }
  
  public static function uriformat($str){
    return str_replace(' ','-',$str);
  }
  
  public static function iconify($word,$icon){
//     $a = ;
    return '<ul id="icons" class="ui-widget"><li class="ui-state-default ui-corner-all" title="'.$word.'"><span class="ui-icon ui-icon-'.$icon.'">'.$word.'</span></li></ul>';
  }
  
  public static function urifuncs($func){
    $uri = new Uri;
    return $uri->$func();
  }
  
  public static function GetOTM($modtable,$table){
             $modelOTM = "fpp_".$modtable.'_Model';
             $modelOTM = new $modelOTM;
             $meta_modelOTM = $modelOTM->Get_Meta();
             foreach($meta_modelOTM as $fieldOTM => $props){
              $ModelIn = substr($props,20,-1);
              if($ModelIn == $table){
                $metaOTM['primary'] = $fieldOTM;
              }else{
                $metaOTM['secondary'] = $fieldOTM ;
              }
             }
               $metaOTM['table'] = $modtable;
               return $metaOTM;
  }

  public static function cryptme($str){
    $salt = Kohana::config('fpp_config.crypt_salt');
    return sha1($salt.$str.$salt);
  }
  
}